package com.example.auth.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.auth.model.DeptModel;
import com.example.auth.service.DeptModelService;
import com.example.auth.service.UserModelService;
import com.example.db.SqlBuilder;
import com.example.db.TableService;
import com.example.db.model.IPage;
import com.example.model.TreeModel;
import com.example.utils.StringUtils;
import com.example.utils.cacheutils.CacheUtils;
import com.example.utils.treeutils.TreeUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @Classname DeptModelServiceImpl
 * @Description TODO
 * @Date 2022/7/31 0:12
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class DeptModelServiceImpl implements DeptModelService {

    @Autowired
    UserModelService userModelService;

    @Autowired
    TableService tableService;

    @Override
    public IPage<DeptModel> pageDeptModelIPage(Integer pageNum, Integer pageSize, JSONObject object) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(DeptModel.class);
        if(object!=null){
            Object deptName=object.get("deptName");
            if(!StringUtils.isEmpty(deptName)){
                sqlBuilder.and("deptName").like(deptName);
            }
        }
        sqlBuilder.page(pageNum,pageSize);
        IPage<DeptModel> deptModelIPage=tableService.getPage(sqlBuilder,DeptModel.class);
        return deptModelIPage;
    }

    @Override
    public DeptModel getDeptModelById(Object id) {
        DeptModel deptModel=tableService.getOneById(id,DeptModel.class);
        return deptModel;
    }

    @Override
    public void updateDeptModelById(DeptModel model) {
        tableService.updateById(model,DeptModel.class);
        CacheUtils.remove(DeptModel.class.getName());
    }

    @Override
    public DeptModel saveDeptModel(DeptModel model) {
        tableService.save(model,DeptModel.class);
        CacheUtils.remove(DeptModel.class.getName());
        return model;
    }

    @Override
    public List<DeptModel> getDeptModelList() {
        Object list = CacheUtils.get(DeptModel.class.getName());
        if(list==null){
            List<DeptModel> deptModels=tableService.getList(DeptModel.class);
            CacheUtils.put(DeptModel.class.getName(),deptModels,3000);
            return deptModels;
        }
        return (List<DeptModel>)list;
    }

    @Override
    public List<TreeModel> getDeptModelTreeList() {
        List<DeptModel> deptModelList=getDeptModelList();
        List<TreeModel> treeModelList=new ArrayList<>(deptModelList.size());
        TreeModel treeModel=null;
        for (int i = 0; i < deptModelList.size(); i++) {
            DeptModel deptModel = deptModelList.get(i);
            treeModel=new TreeModel();
            treeModel.setId(deptModel.getId());
            treeModel.setPid(deptModel.getPid());
            treeModel.setTitle(deptModel.getDeptName());
            treeModelList.add(treeModel);
        }
        List<TreeModel> treeModels=TreeUtils.getTreeModelList(treeModelList,0);
        return treeModels;
    }


    @Override
    public List<DeptModel> getDeptModelChildListById(Object id) {
        List<DeptModel> deptModelList=getDeptModelList();
        List<DeptModel> list=getDeptModelChildList(deptModelList,id);
        return list;
    }

    private List<DeptModel> getDeptModelChildList(List<DeptModel> all,Object id) {
        List<DeptModel> deptModelList=new ArrayList<>();
        all.stream().filter(e->{
            return String.valueOf(e.getPid()).equals(String.valueOf(id));
        }).map((e)->{
            addchlidList(all,e,deptModelList);
            deptModelList.add(e);
            return e;
        }).collect(Collectors.toList());
        return deptModelList;
    }

    private void addchlidList(List<DeptModel> all,DeptModel deptModel,List<DeptModel> list){
        all.stream().filter(e->{
            return String.valueOf(e.getPid()).equals(String.valueOf(deptModel.getId()));
        }).map((e)->{
            addchlidList(all,e,list);
            list.add(e);
            return e;
        }).collect(Collectors.toList());
    }
}
